package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.AdminUser;
import com.ricohgag.mapper.AdminUserMapper;
import com.ricohgag.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    public AdminUser selectByName(String userName){
        AdminUser adminUser = adminUserMapper.selectByName(userName);

        return adminUser;
    }

    @Override
    public AdminUser checkAdminUser(String userName, String password){
        AdminUser adminUser = selectByName(userName);
        if(adminUser!=null && !"".equals(userName.trim())){
            if(password.equals(adminUser.getPassword())){
                return adminUser;
            }
            return null;
        }
        return null;
    }


    /****************************分割**********************************/
    @Override
    public IPage<AdminUser> selectAdminUserPage(String name, Page<AdminUser> page, String startDate, String endDate) throws Exception{
        QueryWrapper<AdminUser> qw = new QueryWrapper<>();
            if(name != null){
                qw.like("username", name);
            }

        if(startDate!=null&&!"".equals(startDate) && endDate!=null&&!"".equals(endDate)){
            qw.between("create_time", startDate, endDate);
        }

        IPage<AdminUser> adminUserIPage = adminUserMapper.selectPage(page, qw);


        return adminUserIPage;
    }

    @Override
    public AdminUser selectAdminUserOne(Integer id){

        return adminUserMapper.selectById(id);
    }

    @Override
    public int updateAdminUser(AdminUser adminUser) throws Exception{
        return adminUserMapper.updateById(adminUser);
    }

    @Override
    public int insertAdminUser(AdminUser adminUser) throws Exception{
        adminUser.setCreateTime(LocalDateTime.now());
        return adminUserMapper.insert(adminUser);
    }

    @Override
    public int deleteAdminUser(int id) throws Exception{
        return adminUserMapper.deleteById(id);
    }


}
